package com.jflm.util;

import java.io.*;
import java.net.*;
import java.security.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import java.io.UnsupportedEncodingException;
public class Client {
	
	private static Logger logger = LogManager.getLogger("Client");

	/*
	 * webservice服务器定义
	 */
	private String serviceURL = "http://sdk2.entinfo.cn:8061/webservice.asmx";

	private String sn = "";// 序列号

	private String password = "";

	private String pwd = "";// 密码
	  Document document = null; 

	  NodeList allNode = null; 
	/*
	 * 构造函数
	 */
	public Client(String sn, String password)
			throws UnsupportedEncodingException {
		this.sn = sn.trim();
		this.password = password.trim();
		this.pwd = this.getMD5(sn + password);
	}

	/*
	 * 方法名称：getMD5 功 能：字符串MD5加密 参 数：待转换字符串 返 回 值：加密之后字符串
	 */
	public String getMD5(String sourceStr) throws UnsupportedEncodingException {
		String resultStr = "";
		try {
			byte[] temp = sourceStr.getBytes();
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(temp);
			// resultStr = new String(md5.digest());
			byte[] b = md5.digest();
			for (int i = 0; i < b.length; i++) {
				char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
						'9', 'A', 'B', 'C', 'D', 'E', 'F' };
				char[] ob = new char[2];
				ob[0] = digit[(b[i] >>> 4) & 0X0F];
				ob[1] = digit[b[i] & 0X0F];
				resultStr += new String(ob);
			}
			return resultStr;
		} catch (NoSuchAlgorithmException e) {
			logger.error("#getMD5()异常", e);
			return null;
		}
	}
	/*
	 * 方法名称：mt 功 能：发送短信 参 数：mobile,content,ext,stime,rrid(手机号，内容，扩展码，定时时间，唯一标识)
	 *	 msgfmt内容编码,0：ASCII串,3：短信写卡操作,4：二进制信息,8：UCS2编码,空或15：含GB汉字.
	 * 返 回 值：唯一标识，如果不填写rrid将返回系统生成的
	 */
	public String mdsmssend(String mobile, String content, String ext, String stime,String rrid,String msgfmt) throws Exception {
		String result = "";
		String soapAction = "http://entinfo.cn/mdsmssend";
		StringBuffer xml = new StringBuffer("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">")
			.append("<soap:Body><mdsmssend  xmlns=\"http://entinfo.cn/\"><sn>").append(sn).append("</sn><pwd>").append(pwd)
			.append("</pwd><mobile>").append(mobile).append("</mobile><content>").append(content).append("</content><ext>").append(ext)
			.append("</ext><stime>").append(stime + "</stime><rrid>").append(rrid).append("</rrid><msgfmt>").append(msgfmt).append("</msgfmt></mdsmssend></soap:Body></soap:Envelope>");

		URL url;
		//URLConnection connection = null;
		//OutputStream out = null;
		try {
			url = new URL(serviceURL);
			URLConnection connection = url.openConnection();
			connection.setConnectTimeout(30000);
			connection.setReadTimeout(120000);
			HttpURLConnection httpconn = (HttpURLConnection) connection;
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			bout.write(xml.toString().getBytes());
			byte[] b = bout.toByteArray();
			httpconn.setRequestProperty("Content-Length", String.valueOf(b.length));
			httpconn.setRequestProperty("Content-Type","text/xml; charset=gb2312");
			httpconn.setRequestProperty("SOAPAction", soapAction);
			httpconn.setRequestMethod("POST");
			httpconn.setDoInput(true);
			httpconn.setDoOutput(true);
			OutputStream out = httpconn.getOutputStream();
			out.write(b);
			out.close();
			InputStreamReader isr = new InputStreamReader(httpconn.getInputStream());
			BufferedReader in = new BufferedReader(isr);
			String inputLine;
			while (null != (inputLine = in.readLine())) {
				Pattern pattern = Pattern.compile("<mdsmssendResult>(.*)</mdsmssendResult>");
				Matcher matcher = pattern.matcher(inputLine);
				while (matcher.find()) {
					result = matcher.group(1);
				}
			}
			return result;
		} catch (Exception e) {
			logger.error("#mdsmssend()异常", e);
			throw e;
		}
	}

	/*
	 * 方法名称：gxmt 功 能：发送短信 参
	 * 数：mobile,content,ext,stime,rrid(手机号，内容，扩展码，定时时间，唯一标识) 返 回
	 *  msgfmt内容编码,0：ASCII串,3：短信写卡操作,4：二进制信息,8：UCS2编码,空或15：含GB汉字.
	 * 值：唯一标识，如果不填写rrid将返回系统生成的
	 */
	public String mdgxsend(String mobile, String content, String ext, String stime,
			String rrid,String msgfmt ) {
		String result = "";
		String soapAction = "http://entinfo.cn/mdgxsend";
		StringBuffer xml = new StringBuffer("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">")
			.append("<soap:Body><mdgxsend  xmlns=\"http://entinfo.cn/\"><sn>").append(sn).append("</sn><pwd>").append(pwd).append("</pwd><mobile>")
			.append(mobile + "</mobile><content>").append(content).append("</content><ext>").append(ext).append("</ext><stime>").append(stime)
			.append("</stime><rrid>").append(rrid).append("</rrid><msgfmt>").append(msgfmt).append("</msgfmt></mdgxsend></soap:Body></soap:Envelope>");

		URL url;
		try {
			url = new URL(serviceURL);

			URLConnection connection = url.openConnection();
			HttpURLConnection httpconn = (HttpURLConnection) connection;
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			bout.write(xml.toString().getBytes());
			byte[] b = bout.toByteArray();
			httpconn.setRequestProperty("Content-Length", String
					.valueOf(b.length));
			httpconn.setRequestProperty("Content-Type",
					"text/xml; charset=gb2312");
			httpconn.setRequestProperty("SOAPAction", soapAction);
			httpconn.setRequestMethod("POST");
			httpconn.setDoInput(true);
			httpconn.setDoOutput(true);

			OutputStream out = httpconn.getOutputStream();
			out.write(b);
			out.close();

			InputStreamReader isr = new InputStreamReader(httpconn
					.getInputStream());
			BufferedReader in = new BufferedReader(isr);
			String inputLine;
			while (null != (inputLine = in.readLine())) {
				Pattern pattern = Pattern
						.compile("<mdgxsendResult>(.*)</mdgxsendResult>");
				Matcher matcher = pattern.matcher(inputLine);
				while (matcher.find()) {
					result = matcher.group(1);
				}
			}
			return result;
		} catch (Exception e) {
			logger.error("#mdgxsend()异常", e);
			return "";
		}
	}

	
}
